package com.dhxt.springcloud.test;

import java.io.IOException;

import org.springframework.http.HttpRequest;
import org.springframework.http.client.ClientHttpRequestExecution;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.util.Base64Utils;

public class BasicAuthorizationInteerceptor implements ClientHttpRequestInterceptor{

	private final String userName;
	
	private final String passWord;
	
	public BasicAuthorizationInteerceptor(String userName ,String passWord) {
		this.userName = userName;
		this.passWord = (passWord == null?"":passWord);
	}
	
	@Override
	public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution exe)
			throws IOException {
		String token = Base64Utils.encodeToString((this.userName+":"+this.passWord).getBytes("utf-8"));
		request.getHeaders().add("Authorization", "Basic"+token);
		return exe.execute(request, body);
	}

}
